﻿using Microsoft.AspNetCore.Mvc;
using MS.Core.BaseProvider;
using MS.Core.Model;
using Net6Shell.Vo;
using ReportDesignerServer.DataModel;
using ReportDesignerServer.IRepository;
using ReportDesignerServer.Repository;
using SqlSugar;

namespace ReportDesignerServer.Services
{
    /// <summary>
    /// 通用字典服务
    /// </summary>
    public class RtmsRecordService : ServiceBase<RtmsRecord, IRtmsRecordRepository>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rtmsRecordRepository"></param>
        public RtmsRecordService(IRtmsRecordRepository rtmsRecordRepository)
        {
            this.Repository = rtmsRecordRepository;
        }



        public Rt QueryRtmsRecord([FromBody] RtmsQueryModel query)
        {
            Expressionable<RtmsRecord> exp = Expressionable.Create<RtmsRecord>();
            if (query.StartTime != null && query.EndTime != null)
            {
                exp.And(it => it.RECORD_TIME >= ((DateTime)query.StartTime).Date);
                exp.And(it => it.RECORD_TIME < ((DateTime)query.EndTime).Date.AddDays(1));
            }
            else
                return Rt.Fail("请选择时间段！");

            if (!string.IsNullOrEmpty(query.SearchTest))
            {
                exp.And(it => it.NAME.Contains(query.SearchTest) || it.BED.Contains(query.SearchTest));
            }

            var list = Repository.GetListByWhere(exp);
            return Rt.SuccessObj(list);
        }



        public Rt GetRtmsRecordById(string id)
        {
            long lid = 0;
            if (long.TryParse(id, out lid))
            {
                Expressionable<RtmsRecord> exp = Expressionable.Create<RtmsRecord>();
                exp.And(it => it.ID == lid);
                var model = Repository.GetListByWhere(exp).FirstOrDefault();
                return Rt.SuccessObj(model);
            }
            else
            {
                return Rt.Error();
            }
        }


        public Rt SaveRtmsRecord([FromBody] RtmsRecord model)
        {
            long res = 0;
            if (model != null)
            {
                if (model.ID == 0)
                {
                    res = Repository.ExecuteReturnSnowflakeId(model);
                }
                else
                {
                    res = Repository.Update(model);
                }
            }
            if (res > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }


        public Rt SaveRtmsRecords([FromBody] List<RtmsRecord> model)
        {
            List<long> ids = new List<long>();
            if (model != null)
            {
                ids = Repository.ExecuteRecordsSnowflakeId(model);
            }
            if (ids != null && ids?.Count > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }


        public Rt DelRtmsRecord([FromBody] RtmsRecord model)
        {
            long res = 0;
            if (model != null)
            {
                long lid = 0;
                res = Repository.Delete(model);

            }
            if (res > 0)
            {
                return Rt.Success();
            }
            else
            {
                return Rt.Error();
            }
        }
    }
}
